//
//  WJNetTool.m
//  WJBSApp
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJNetTool.h"
#import "AFNetworking.h"

@implementation WJNetTool

+ (void)GET:(NSString *)url body:(id)body header:(NSDictionary *)header response:(WJResponseStyle)responseStyle success:(successBlock)success failure:(failureBlock)failure
{
    // 1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.设置请求头
    if (header) {
        for (NSString *key in header) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // HTTPS
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    // 3.返回值类型
    switch (responseStyle) {
        case WJJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case WJDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case WJXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 4.设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    // 5. UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 6.使用AFN进行网络请求
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}

+ (void)POST:(NSString *)url
        body:(id)body
   bodyStyle:(WJBodyStyle)bodyStyle
      header:(NSDictionary *)header
    response:(WJResponseStyle)responseStyle
     success:(successBlock)success
     failure:(failureBlock)failure
{
    // 1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2. 设置Body的数据类型
    switch (bodyStyle) {
        case WJBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case WJBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    
    // 3.设置请求头
    if (header) {
        for (NSString *key in header) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // HTTPS
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    // 4.返回值类型
    switch (responseStyle) {
        case WJJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case WJDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case WJXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 5.设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    // 6. UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 7.使用AFN进行网络请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}











@end
